package org.dlsu.pdfgen.gui;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dlsu.pdfgen.exceptions.InvalidXMLFileFormatException;
import org.dlsu.pdfgen.io.Revised2011XMLReader;
import org.dlsu.pdfgen.io.XMLReader;
import org.dlsu.pdfgen.struct.Syllabus;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * This class is the window for the file chooser dialog.
 * 
 * @author Lexi Valencia
 * 
 */

public class EditXMLWindow extends javax.swing.JDialog{
    
    public EditXMLWindow(java.awt.Frame parent, boolean modal) {
    	super(parent,modal);
    	initComponents();
		Dimension dim = getToolkit().getScreenSize();
        Rectangle abounds = getBounds();
        initComponents();
        setLocationRelativeTo(null);
        this.setVisible(true);
        fileChooser = new JFileChooser();
    }
                         
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        chooseFileLabel = new javax.swing.JLabel();
        fileTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        uploadButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        editLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edit Existing XML");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        chooseFileLabel.setText("Choose File:");

        browseButton.setText("Browse...");
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        uploadButton.setText("Upload");
        uploadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					uploadButtonActionPerformed(evt);
				} catch (SAXException | IOException | InvalidXMLFileFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(chooseFileLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cancelButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(uploadButton, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                    .addComponent(browseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chooseFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseButton))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(uploadButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        editLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        editLabel.setText("Edit Existing XML File");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(17, Short.MAX_VALUE)
                .addComponent(editLabel)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {           
    	this.fileChooser = new JFileChooser();

    	FileNameExtensionFilter filter = new FileNameExtensionFilter("XML Files", "xml");
    	fileChooser.setFileFilter(filter);
    	
    	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    	if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			if (fileChooser.getSelectedFile().getName().endsWith(".xml")) {
				System.out.println("Valid XML file.");
				fileTextField.setText(fileChooser.getSelectedFile()
						.getAbsolutePath());
				fileTextField.setEditable(false);
				filePath = fileTextField.getText();
			} else {
				System.err.println("Not an XML file.");
				JOptionPane.showMessageDialog(this,
						"The file you selected was not an XML file.",
						"File Extension Mismatch", JOptionPane.ERROR_MESSAGE);
			}
		}
    }                                         

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
            if (JOptionPane.showConfirmDialog(null,
                    "Cancel file editing?", "Cancel",
                    JOptionPane.YES_NO_OPTION) == 0) {
                    dispose();
            }
    }                                         

    private void uploadButtonActionPerformed(java.awt.event.ActionEvent evt) throws SAXException, IOException, InvalidXMLFileFormatException, ParserConfigurationException {     
    	if(!fileTextField.getText().isEmpty()){
    		dispose();
    		Syllabus syllabus = new Syllabus();
        	syllabus = Revised2011XMLReader.getInstance().readSyllabus(filePath);
        	CreateXMLWindow editXMLWindow = new CreateXMLWindow(parent,true, true);
    	}
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton browseButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField fileTextField;
    private javax.swing.JLabel editLabel;
    private javax.swing.JLabel chooseFileLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton uploadButton;
    private JFileChooser fileChooser;
    private String filePath;
    private java.awt.Frame parent;
    // End of variables declaration                   

}
